<?php

namespace app\api\controller;

use think\Db;
use think\Request;
use app\api\model\Collect as collectModel;

class Card extends Nocommon
{
    //是商户的接口
    public function is_merchant()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            //查询是否是有店铺
            $where['food_store_id'] = ['neq', 0];
            $where['tour_id'] = ['neq', 0];
            $where['team_id'] = ['neq', 0];
            $where['hotel_id'] = ['neq', 0];
            $shop = Db::name('user')->field('food_store_id,tour_id,team_id,hotel_id')->where('uid', $post['uid'])->find();
            if ($shop) {
                $c = [];
                if (!empty($shop['food_store_id'])) {
                    $food = Db::name('food_store')
                        ->field('food_store_id as id,food_store_name as name,food_tag as tag,food_store_tel as tel,food_store_address,city')
                        ->where('food_store_id', $shop['food_store_id'])
                        ->find();
                    $food['address'] = $food['city'] . $food['food_store_address'];
                    $food['status'] = 1;
                    if (!empty($food['tag'])) {
                        $food['tag'] = explode(',', $food['tag']);
                    } else {
                        $food['tag'] = [];
                    }
                    $food['image'] = Db::name('food_store_img')->field('img_path')->where('store_id', $shop['food_store_id'])->select();
                    if (empty($food['image'])) {
                        $food['image'] = [];
                    }
                    unset($food['food_store_address']);
                    unset($food['city']);
                    array_push($c, $food);
                }
                if (!empty($shop['tour_id'])) {
                    $tour = Db::name('tours')
                        ->field('tour_id as id,tour_name as name,tour_tag as tag,tour_tel as tel,address,city')
                        ->where('tour_id', $shop['tour_id'])
                        ->find();
                    $tour['address'] = $tour['city'] . $tour['address'];
                    if (!empty($tour['tag'])) {
                        $tour['tag'] = explode(',', $tour['tag']);
                    } else {
                        $tour['tag'] = [];
                    }
                    $tour['image'] = Db::name('tour_img')->field('img_path')->where('tour_id', $shop['tour_id'])->select();
                    if (empty($food['image'])) {
                        $food['image'] = [];
                    }
                    $tour['status'] = 2;
                    unset($tour['city']);
                    array_push($c, $tour);
                }
                if (!empty($shop['team_id'])) {
                    $team = Db::name('team')
                        ->field('team_id as id,team_name as name,team_phone as tel,team_address,city')
                        ->where('team_id', $shop['team_id'])
                        ->find();
                    $team['address'] = $team['city'] . $team['team_address'];
                    $team['tag'] = [];
                    $team['image'] = Db::name('team_img')->field('img_path')->where('team_id', $shop['team_id'])->select();
                    if (empty($food['image'])) {
                        $food['image'] = [];
                    }
                    $team['status'] = 3;
                    unset($team['team_address']);
                    unset($team['city']);
                    array_push($c, $team);
                }
                if (!empty($shop['hotel_id'])) {
                    $hotel = Db::name('hotel')
                        ->field('hotel_id as id,hotel_name as name,hotel_tag as tag,hotel_tel as tel,address,city')
                        ->where('hotel_id', $shop['hotel_id'])
                        ->find();
                    $hotel['address'] = $hotel['city'] . $hotel['address'];
                    if (!empty($hotel['tag'])) {
                        $hotel['tag'] = explode(',', $hotel['tag']);
                    } else {
                        $hotel['tag'] = [];
                    }
                    $hotel['image'] = Db::name('hotel_img')->field('img_path')->where('hotel_id', $shop['hotel_id'])->select();
                    if (empty($food['image'])) {
                        $food['image'] = [];
                    }
                    $hotel['status'] = 4;
                    unset($hotel['city']);
                    array_push($c, $hotel);
                }


                $this->apiReturn('200', '成功', $c);
            } else {
                $this->apiReturn('400', '店铺不存在');
            }
        }
    }

//    //获取图片
//    public function merchat_image()
//    {
//        if (Request::instance()->isPost()) {
//            $post = Request::instance()->post();
//            $status = $post['status'];
//            $id = $post['id'];
//            //status = 1 美食店铺  2景点 3旅行社 4酒店
//            if($status==1){
//                $res = Db::name('food_store_img')->field('id,img_path')->where('store_id',$id)->select();
//            }
//            if($status==4){
//                $res = Db::name('hotel_img')->field('id,img_path')->where('hotel_id',$id)->select();
//            }
//            if($status==3){
//                $res = Db::name('team_img')->field('id,img_path')->where('team_id',$id)->select();
//            }
//            if($status==2){
//                $res = Db::name('tour_img')->field('id,img_path')->where('tour_id',$id)->select();
//            }
//            $this->apiReturn('200','成功',$res);
//        }
//    }
    /*
     * 选择店铺
     */
    public function merchant()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            $type = input('type', '1', 'trim');
            //景点
            if ($type == 1) {
                $res = Db::name('tours')->field('tour_name as name')->where('is_pass', 1)->where('is_display', 0)->where('is_del', 0)->select();
            }
            //酒店
            if ($type == 2) {
                $res = Db::name('hotel')->field('hotel_name as name')->where('is_pass', 1)->where('is_display', 0)->where('is_del', 0)->select();
            }
            //店铺
            if ($type == 3) {
                $res = Db::name('food_store')->field('food_store_name as name')->where('is_pass', 1)->where('is_del', 0)->select();
            }
            //旅行社
            if ($type == 4) {
                $res = Db::name('team')->field('team_name as name')->where('is_pass', 1)->where('is_display', 0)->where('is_del', 0)->select();
            }
            $this->apiReturn('200', '成功', $res);
        }
    }

    //个人电子名片添加
    public function insert_card()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            $data = [
                'store_name' => $post['name'],
                'zhiwei' => $post['job'],
                'rename' => $post['rename'],
                'phone' => $post['phone'],
                'type' => $post['type'],
                'uid' => $post['uid'],
                'create_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::name('my_card')->insert($data);
            if ($res) {
                $this->apiReturn('200', '申请成功');
            } else {
                $this->apiReturn('400', '申请失败');
            }
        }
    }

    //个人电子名片
    public function card_info()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            //查询
            $info = Db::name('my_card')->field('id,store_name,rename,phone,zhiwei,icon,type')->where('uid', $post['uid'])->find();
            if ($info['type'] == 1) {
                $res = Db::name('tours')->field('city,address')->where('tour_name', $info['store_name'])->find();
                $info['address'] = $res['city'] . $res['address'];
            }
            if ($info['type'] == 2) {
                $res = Db::name('hotel')->field('city,address')->where('hotel_name', $info['store_name'])->find();
                $info['address'] = $res['city'] . $res['address'];
            }
            if ($info['type'] == 3) {
                $res = Db::name('food_store')->field('food_store_address,city')->where('food_store_name', $info['store_name'])->find();
                $info['address'] = $res['city'] . $res['food_store_address'];
            }
            if ($info['type'] == 4) {
                $res = Db::name('team')->field('city,team_address')->where('team_name', $info['store_name'])->find();
                $info['address'] = $res['city'] . $res['team_address'];
            }
            if ($info) {
                $info['image'] = Db::name('card_img')->field('image')->where('uid', $post['uid'])->select();
            } else {
                $info['image'] = [];
            }
            unset($info['type']);
            $this->apiReturn('200', '成功', $info);
        }
    }

    //修改头像
    public function update_icon()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            //修改头像
            $res = Db::name('my_card')->where('id', $post['id'])->setField('icon', $post['icon']);
            if ($res) {
                $this->apiReturn('200', '修改成功');
            } else {
                $this->apiReturn('400', '修改失败');
            }
        }
    }

    //查看照片
    public function card_img_show()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            if ($post['type'] == 1) {
                $res = Db::name('card_img')->field('id,image')->where('uid', $post['uid'])->select();
                if ($res) {
                    foreach ($res as $key => $value) {
                        $res[$key]['image'] = addWaterByField($value['image']);
                    }
                }

            } else if ($post['type'] == 2) {
                $status = $post['status'];
                $id = $post['id'];
                if ($status == 1) {
                    $res = Db::name('food_store_img')->field('id,img_path as image')->where('store_id', $id)->select();
                }
                if ($status == 4) {
                    $res = Db::name('hotel_img')->field('id,img_path as image')->where('hotel_id', $id)->select();
                }
                if ($status == 3) {
                    $res = Db::name('team_img')->field('id,img_path as image')->where('team_id', $id)->select();
                }
                if ($status == 2) {
                    $res = Db::name('tour_img')->field('id,img_path as image')->where('tour_id', $id)->select();
                }
                if ($res) {
                    foreach ($res as $key => $value) {
                        $res[$key]['image'] = addWaterByField($value['image']);
                    }
                }

            }

            $this->apiReturn('200', '成功', $res);
        }
    }

    //删除照片墙
    public function card_img_delete()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            if ($post['type'] == 1) {
                //查询所有ID
                $id = $post['id'];
                $data = Db::name('card_img')->where("id in ($id)")->delete();

            } else if ($post['type'] == 2) {
                //查询所有ID
                $id = $post['id'];
                $status = $post['status'];
                if ($status == 1) {
                    $data = Db::name('food_store_img')->where("id in ($id)")->delete();
                }
                if ($status == 4) {
                    $data = Db::name('hotel_img')->where("id in ($id)")->delete();
                }
                if ($status == 3) {
                    $data = Db::name('team_img')->where("id in ($id)")->delete();
                }
                if ($status == 2) {
                    $data = Db::name('tour_img')->where("id in ($id)")->delete();
                }
            }
            if ($data) {
                $this->apiReturn('200', '删除成功');
            } else {
                $this->apiReturn('400', '删除失败');
            }

        }
    }

    //添加照片墙
    public function card_img_insert()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            $status = $post['status'];
            $id = $post['id'];
            $data = explode(',', $post['image']);
            if ($post['type'] == 1) {
                foreach ($data as $k => $v) {
                    $insert[$k] = [
                        'uid' => $post['uid'],
                        'image' => $v,
                        'card_id'=>$post['id'],
                        'create_time' => date('Y-m-d H:i:s'),
                    ];

                }
                $res = Db::name('card_img')->insertAll($insert);
            } else if ($post['type'] == 2) {
                if ($status == 1) {
                    foreach ($data as $k => $v) {
                        $insert[$k] = [
                            'store_id' => $id,
                            'img_path' => $v,
                            'create_time' => date('Y-m-d H:i:s'),
                        ];

                    }
                    $res = Db::name('food_store_img')->insertAll($insert);
                }
                if ($status == 4) {
                    foreach ($data as $k => $v) {
                        $insert[$k] = [
                            'hotel_id' => $id,
                            'img_path' => $v,
                            'create_time' => date('Y-m-d H:i:s'),
                        ];

                    }
                    $res = Db::name('hotel_img')->insertAll($insert);
                }
                if ($status == 3) {
                    foreach ($data as $k => $v) {
                        $insert[$k] = [
                            'team_id' => $id,
                            'img_path' => $v,
                            'create_time' => date('Y-m-d H:i:s'),
                        ];

                    }
                    $res = Db::name('team_img')->insertAll($insert);
                }
                if ($status == 2) {
                    foreach ($data as $k => $v) {
                        $insert[$k] = [
                            'tour_id' => $id,
                            'img_path' => $v,
                            'create_time' => date('Y-m-d H:i:s'),
                        ];

                    }
                    $res = Db::name('tour_img')->insertAll($insert);
                }
            }

            if ($res) {
                $this->apiReturn('200', '添加成功');
            } else {
                $this->apiReturn('400', '添加失败');
            }
        }
    }

    /*
     * 获取职位
     */
    public function card_zw()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            $res = Db::name('card_zw')->field('name')->where('is_display', 0)->select();
            if ($res) {
                $this->apiReturn('200', '成功', $res);
            } else {
                $this->apiReturn('400', '失败');
            }
        }
    }
}
